<template>
  <el-container class="container">
    <el-header>
      <nagivation />
    </el-header>
    <el-main style="text-align:center;">
      <h1>开始写文章了</h1>
      <el-divider><i class="el-icon-mobile-phone"></i></el-divider>
      <div style="margin:20px">
        <el-form :inline="true" :model="form" class="demo-form-inline" :rules="rules" ref="ruleForm">
          <el-form-item label="文章分类" prop="CategoryValue">
            <el-select v-model="form.CategoryValue" clearable placeholder="请选择文章分类">
              <el-option v-for="category in categoryEntityList" :key="category.categoryId" :label="category.title"
                :value="category.categoryId">
              </el-option>
              <el-option key="-1" value=""><i class="el-icon-plus" @click="showGetAddCategory">
                  没有喜欢的分类吗,点击添加</i>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文章标签" prop="TagValue">
            <el-select v-model="form.TagValue" multiple filterable allow-create default-first-option
              placeholder="请选择文章标签">
              <el-option v-for="tag in tagEntityList" :key="tag.id" :label="tag.name" :value="tag.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文章标题" prop="TitleValue">
            <el-input v-model="form.TitleValue" placeholder="请输入内容" style="display:inline-block">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <tinymce ref="tinymceContent" />
      </div>
      <el-dialog :visible.sync="centerDialogVisible" center>
        <h1>请输入你想要的分类</h1>
        <el-input placeholder="请输入内容" v-model="categoryInput" clearable>
        </el-input>
        <el-button @click="getAddCategory">提交</el-button>
      </el-dialog>
    </el-main>
  </el-container>
</template>

<script>
import nagivation from '../component/nagivation.vue';
import tinymce from '@/component/tinymce'
import api from '@/utils/api.js'
export default {
  name: 'writearticle',
  data() {
    var validateTitle = (rule, value, callback) => {
      if (value != null) {
        callback()
      } else {
        callback(new Error('标题不能为空'))
      }
    };
    var validateTag = (rule, value, callback) => {
      if (value != null) {
        callback()
      } else {
        callback(new Error('标签不能为空'))
      }

    };
    var validateCategory = (rule, value, callback) => {
      if (value != null) {
        callback()
      } else {
        callback(new Error('分类不能为空'))
      }

    };
    return {
      categoryInput: '',
      categoryEntityList: [],
      tagEntityList: [],
      centerDialogVisible: false,
      tagLength:0,
      form: {
        TitleValue: '',
        TagValue: '',
        CategoryValue: ''
      },
      rules: {
        TitleValue: [
          { validator: validateTitle, trigger: 'blur' }
        ],
        TagValue: [
          { validator: validateTag, trigger: 'blur' }
        ],
        CategoryValue: [
          { validator: validateCategory, trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    tinymce,
    nagivation
  },
  created() {
    if (this.categoryEntityList.length < 1) {
      this.getAllCategory()
    }
    var username = this.$store.state.user.username
    console.log('username是', this.$store.state.user.username)
    if (username == "") {
      console.log('执行了获取信息')
      this.getUserInfo()
    }
  },
  methods: {
    showGetAddCategory(){
         this.centerDialogVisible = true
    },
    getAddCategory() {
      var categoryContent = this.categoryInput
      if (categoryContent != null&&categoryContent!='') {
        api.addCategory(categoryContent).then(res => {
          if (res.data) {
            this.categoryInput=''
            this.getAllCategory()
             this.centerDialogVisible = false
            this.$message({
              type: 'success',
              message: '添加成功'
            })
          }else{
              this.$message({
              type: 'error',
              message: '添加失败'
            })
          }
        })
      } else {
        this.$message({
          type: 'error',
          message: '输入的分类不能是空的,想好再输入吧'
        })
      }
    },
    goAddCategory(item) {
      console.log('测试', item)
    },
    go() {
      console.log('form是', this.form)
    },
    sendArticleMessage() {
      this.$refs.form.validate(valida => {
        if (valida) {
          console.log('数据时', this.form)
        }
      })
    },
    getAllCategory() {
      api.getAllCategory().then(res => {
        this.categoryEntityList = res.data
      }).catch(error => {
        console.log(error)
      })
    },
    getUserInfo() {
      this.$store.dispatch('GetInfo').then(() => {
      })
    },
  },
  watch: {
    'form': {
      handler(val, oval) {
        console.log('监听',val)
        if (val.CategoryValue != "" ) {
          console.log('category是', val.CategoryValue)
          var categoryId = val.CategoryValue
          if(val.TagValue.length<1){
            api.byCategoryGetTag(categoryId).then(res => {
              console.log('测试数据是',res.data)
              this.tagEntityList = res.data
              if(res.data.length>0){
              this.tagLength=res.data.length
              }
            }).catch(error => {
              console.log(error)
            })
          }
            if (val.CategoryValue != "" && val.TagValue.length > 0 && val.TitleValue != "") {
              console.log('数据是', val)
              var userId = this.$store.state.user.userId
              this.$refs.tinymceContent.getArticleTag(val, userId)
            }
          
        }
      },
      deep: true
    }
  }
}
</script>

<style scoped>
.container {
  padding: 0px;
  margin: 0px;
  overflow: hidden;
}
</style>